<template>
  <div class="contenBox">
    <!-- 头部搜索 -->
    <div class="first">
      <el-card class="box-card">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="专业名称">
            <el-input v-model="maname" placeholder="请输入专业名称"></el-input>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="createTime"
              align="center"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择时间"
              @focus="selectDate"
              @blur="closeSelect"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="所属院校">
            <el-cascader
              v-model="form1.colleageId"
              :options="options"
              :props="{ expandTrigger: 'true', value: 'id' }"
              clearable
              @change="handleChange"
            ></el-cascader>
            <!-- <el-cascader
              :options="options"
              v-model="form1.colleageId"
              :props="{ checkStrictly: true ,value: 'id'}"
              clearable
              @change="handleChange"
              :show-all-levels="false"
            ></el-cascader> -->
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="changeSearch"
              style="width: 100px; background: #7bb4d0; border: none"
            >
              <i class="el-icon-search"></i>
              搜索</el-button
            >
            <el-button
              type="success"
              @click="refreshInput"
              style="width: 100px; background: #6ccfcc; border: none"
            >
              <i class="el-icon-refresh"></i>
              重置</el-button
            >
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <!-- 列表栏 -->
    <div class="list">
      <h3 style="color: #3c98d9; margin-top: 30px">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;管理列表
      </h3>
      <div style="line-height: 80px; margin-left: 10px">
        <el-button
          @click="addmajor"
          type="primary"
          style="background: #5ca5d2; border: none"
        >
          <i class="el-icon-plus"></i>
          新增</el-button
        >
        <el-button
          type="danger"
          @click="moreDelete"
          style="background: #f1818d; border: none"
        >
          <i class="el-icon-close"></i>
          批量删除</el-button
        >
      </div>
    </div>
    <div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 94%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="序号" type="index" width="120">
        </el-table-column>
        <el-table-column prop="majorName" label="专业名称" width="120">
        </el-table-column>
        <el-table-column prop="colleageName" label="所属院校">
        </el-table-column>
        <el-table-column prop="createBy" label="创建人"> </el-table-column>
        <el-table-column prop="createTime" label="创建时间"> </el-table-column>
        <el-table-column prop="status" label="启用状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#C2C2C2"
              active-value="0"
              inactive-value="1"
              @change="updateIsUsing(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <div>
              <!-- <el-button style="width:40px;border:none;color:#1990FF" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
              <el-button style="width:40px;border:none;color:#1990FF" @click="del(scope.row)">删除</el-button> -->
              <el-button type='text' @click="handleEdit(scope.$index, scope.row)">修改</el-button>
                  <el-button type='text' @click="del(scope.row)">删除</el-button>     
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.pageNum"
        :page-sizes="[2, 3, 4, 5]"
        :page-size="query.pageSize"
        prev-text="上一页"
        next-text="下一页"
        layout=" prev, pager, next"
        :total="query.total"
      ></el-pagination>

      <div class="jumper">
        跳到第
        <input type="text" ref="pageNum" :value="query.pageNum" />
        页
      </div>
      <button @click="goPage">确定</button>
    </div>
    <div class="dialog">
      <!-- 新增课程 -->
      <el-dialog
        title="添加专业"
        :visible.sync="dialogFormVisible"
        width="700px"
        height="500px"
      >
        <el-form
          :model="form"
          :rules="rules"
          ref="form"
          label-width="200px"
          class="demo-ruleForm"
        >
          <el-form-item label="专业名称：" prop="majorName">
            <el-input
              v-model="form.majorName"
              style="width: 300px"
              placeholder="请输入专业名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="所属学院：" prop="colleageId">
            <el-select
              v-model="form.colleageId"
              clearable
              placeholder="请选择"
              @clear="infoHandleClear"
              ref="infoSelectUpResId"
            >
              <el-option hidden :value="form.colleageId" :label="infoUpResName">
              </el-option>
              <el-tree
                :data="options"
                :props="defaultProps"
                :expand-on-click-node="false"
                :check-on-click-node="true"
                @node-click="infoHandleNodeClick"
              >
              </el-tree>
            </el-select>
            <!-- <el-cascader
              style="width: 300px"
              :options="options"
              v-model="form.colleageId"
              :props="{ expandTrigger: 'true', value: 'id' }"
              clearable
              @change="handleChange"
   
              :show-all-levels="false"
            ></el-cascader> -->
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="cancel" @click="dialogFormVisible = false"
            >取 消</el-button
          >
          <el-button class="determine" type="primary" @click="addBtn"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </div>
    <!-- 修改弹框 -->
    <el-dialog
      title="修改专业"
      :visible.sync="editFlag"
      width="700px"
      height="500px"
    >
      <div class="addInput">
        <p class="collegeName">专业名称：</p>
        <el-input
          v-model="editform.majorName"
          placeholder="请输入内容"
        ></el-input>
      </div>
      <br />
      <div class="addSelect">
        <p class="superiorCollege">所属院校：</p>
        <el-select
              v-model="editform.colleageId"
              clearable
              placeholder="请选择"
              @clear="infoHandleClear"
              ref="infoSelectUpResId"
            >
              <el-option hidden :value="editform.colleageId" :label="infoUpResName">
              </el-option>
              <el-tree
                :data="options"
                :props="defaultProps"
                :expand-on-click-node="false"
                :check-on-click-node="true"
                @node-click="infoHandleNodeClick"
              >
              </el-tree>
            </el-select>
      </div>
      <br />
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFlag = false">取 消</el-button>
        <el-button type="primary" @click="editBtn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  majorListApi,
  majorAddApi,
  majorDelApi,
  majorEditApi,
} from "@/api/basics/major";
import { colleageTreeDataApi } from "@/api/basics/college";
export default {
  data() {
    return {
      rules: {
        colleageId: [
          { required: true, message: "请选择所属院校", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "请输入创建人", trigger: "change" },
        ],
      },
      createTime: "",
      value2: "",
      tableData: [],
      options: [],
      value: "",
      maname: "",
      query: {
        pageNum: 1,
        pageSize: 6,
        total: 0,
      },
      dialogFormVisible: false,
      editFlag: false,
      form: {
        majorName: "",
        status: 1,
        colleageId: "",
      },
      // 搜索院校
      form1: {
        colleageId: "",
      },
      ids: "",
      editform: {
        majorId: "",
        majorName: "",
        status: 1,
        colleageId: []
      },
      infoUpResName:'',
      // 树组件
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  mounted() {
    this.majorList();
    this.colleageTree();
  },
  methods: {
    handleChange(value) {
      console.log(value);
    },
    infoHandleNodeClick(data) {
      console.log(data);
      // 这里主要配置树形组件点击节点后，设置选择器的值；自己配置的数据，仅供参考
      this.infoUpResName = data.label;
      this.form.colleageId = data.id;
      // 选择器执行完成后，使其失去焦点隐藏下拉框的效果
      this.$refs.infoSelectUpResId.blur();
      // this.editSelectValueHandler(data.id)
      // this.majorId = ''
      // this.majorSelect(data.id)

    },
    infoHandleClear() {
      // 将选择器的值置空
      this.infoUpResName = "";
      this.form.colleageId = "";
    },
    //渲染列表数据
    majorList() {
      majorListApi(this.query)
        .then((res) => {
          console.log(res);
          this.tableData = res.rows;
          this.query.total = res.total;
        })
        .catch(() => {});
    },
    colleageTree() {
      colleageTreeDataApi()
        .then((res) => {
          //   this.loading = false
          console.log(res, "所属学院");
          this.options = res.data;
          console.log("11", this.options);
        })
        .catch(() => {
          //   this.loading = false
        });
    },
    // 搜索
    changeSearch() {
      console.log(this.form1.colleageId !== "");
      var obj = {};
      console.log(obj);
      if (this.form1.colleageId !== "") {
        obj = {
          majorName: this.maname,
          colleageId: this.form1.colleageId.join(","),
          createTime: this.createTime,
        };
      } else {
        obj = {
          majorName: this.maname,
          colleageId: "",
          createTime: this.createTime,
        };
      }
      majorListApi(obj).then((res) => {
        console.log("222", res);
        this.tableData = res.rows;
        this.query.total = res.total;
      });
    },
    //选中
    handleSelectionChange(val) {
      var arrTostring = [];
      val.forEach((item) => {
        if (arrTostring.indexOf(item.id) === -1) {
          arrTostring.push(item.majorId);
        }
      });
      this.ids = arrTostring.toString();
    },
    // 分页
    // 改变每页条数
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.query.pageSize = val;
      this.majorList();
    },
    //切换页数，执行方法
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.query.pageNum = val;
      this.majorList();
    },
    goPage() {
      let { value } = this.$refs.pageNum;
      console.log("1", value);
      this.query.pageNum = Number(value);
      this.majorList();
    },
    //添加
    addmajor() {
      this.dialogFormVisible = true;
      this.form.majorName = "";
      this.form.colleageId = "";
      this.form.createTime = "";
      this.form.createBy = "";
      this.infoUpResName = ''
    },
    addBtn() {
      if (this.form.majorName == "") {
        this.$message.error("请输入专业名称");
      } else if (this.form.colleageId == "") {
        this.$message.error("请选择院校");
      } else {
        console.log(this.form);
        // this.form.colleageId = this.form.colleageId.toString();
        // this.form.colleageId= this.form.colleageId
        majorAddApi(this.form)
          .then((res) => {
            console.log(res);
            console.log(
              "321",
              this.form.majorName,
              this.form.colleageId,
              this.form.colleageName
            );
            this.$message.success("添加成功");
            this.majorList();
            this.dialogFormVisible = !this.dialogFormVisible;
          })
          .catch(() => {});
      }
    },
    // 重置
    refreshInput() {
      this.maname = "";
      this.createTime = "";
      this.form1.colleageId = "";
      this.majorList();
    },
    //删除
    del(row) {
      if (row.majorName != "") {
        this.$confirm("确认删除记录吗?", "提示", {
          type: "warning",
        })
          .then(() => {
            majorDelApi({ ids: JSON.stringify(row.majorId) }).then((res) => {
              console.log(res);
              this.$message.success("删除成功");
              this.majorList();
            });
          })
          .catch(() => {});
      } else {
        this.$message({
          message: "该学院已有专业不能删除",
          type: "warning",
        });
      }
    },
    //批量删除
    moreDelete() {
      console.log(this.ids);
      this.$confirm("确认删除记录吗?", "提示", {
        type: "warning",
      })
        .then(() => {
          majorDelApi({ ids: this.ids }).then((res) => {
            console.log(res);
            this.majorList();
            this.$message.success("删除成功");
          });
        })
        .catch(() => {});
    },
    handleEdit(index, row) {
      console.log(row);
      let arr = [];
      row.colleageId.split(",").forEach((ele) => {
        arr.push(Number(ele));
      });
      this.editFlag = true;
      this.editform.majorName = row.majorName;
      this.editform.status = row.status;
      // this.editform.colleageId = row.colleageId;
      this.editform.colleageId = row.colleageName;
      this.editform.majorId = row.majorId;
      console.log("222", index, row);
      console.log("222222", this.editform.colleageId);
    },
    //修改功能
    editBtn() {
      this.editFlag = false;
      let data = {
        majorId: this.editform.majorId,
        majorName: this.editform.majorName,
        colleageId: this.editform.colleageId.join(","),
      };
      majorEditApi(data).then((res) => {
        console.log(res);
        this.$message.success("添加成功");
        this.majorList();
      });
    },
    //修改状态
    updateIsUsing(row) {
      majorEditApi({ status: row.status, majorId: row.majorId });
      this.$message.success("修改成功");
    },
    //选择时间
    selectDate() {
      this.date = true;
    },

    // 取消选择时间
    closeSelect() {
      this.date = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.contenBox {
  width: 104%;
  height: 100%;
  background-color: #f3f2f7;
  box-sizing: border-box;
  margin-left: 12px;
}
.first {
  display: flex;
  width: 94%;
}
.box-card {
  width: 100%;
}
.search {
  float: right;
  margin-top: -60px;
}
.list {
  width: 93%;
  display: flex;
  justify-content: space-between;
}
.el-icon-edit-outline {
  color: #94ccea;
  font-size: 20px;
  font-weight: bold;
}
.el-icon-delete {
  color: #f75572;
  font-size: 20px;
  font-weight: bold;
  margin-left: 15px;
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: right;
  margin-top: 20px;
  margin-right: 150px;
  .jumper {
    font-size: 14px;
    margin-left: 20px;
    margin-right: 10px;
    display: flex;
    align-items: center;
    input {
      width: 40px;
      height: 26px;
      margin: 0 5px;
      text-align: center;
      border: solid 1px #e6e6e6;
      border-radius: 5px;
      color: #666;
      outline-style: none;
    }
  }
  button {
    height: 28px;
    line-height: 28px;
    border-radius: 5px;
    background-color: #4895db;
    border: solid 1px #4895db;
    color: #fff;
    font-size: 14px;
    padding: 0 8px;
  }
}
::v-deep .el-dialog {
  width: 700px;
  height: 500px;
  border-radius: 10px;
  font-weight: 600;
  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.3s;
  }
  
  .message {
    position: absolute;
    left: 40%;
    top: 50%;
    width: 140px;
    height: 50px;
    text-align: center;
    line-height: 50px;
    color: #fff;
    border-radius: 10px;
    background-color: rgba(0, 0, 0, 0.4);
  }
  .fade-enter,
            .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
    opacity: 0;
  }
  .el-dialog__header {
    border-radius: 10px 10px 0 0;
    text-align: center;
    background-color: rgb(72, 149, 219);
    .el-dialog__title {
      color: #fff;
    }
    .el-dialog__headerbtn .el-dialog__close {
      color: #fff;
    }
  }
  .addInput {
    display: flex;
    justify-content: center;
    align-items: center;
    .collegeName {
      width: 70px;
    }
    .el-input {
      width: 300px;
    }
  }
  .addSelect {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 660px;
    // justify-content: space-around;
    .el-cascader {
      width: 300px;
    }
    .superiorCollege {
      width: 70px;
    }
    .el-select {
      width: 300px;
    }
  }
  .el-dialog__body {
    height: 290px;
    .el-form-item__content .el-select{
      width: 300px;
    }
  }
  .el-dialog__footer {
    text-align: center;
    button {
      width: 100px;
      height: 35px;
      border-radius: 16px;
      line-height: 0;
    }
    .cancel {
      border: none;
      color: #848383;
      background-color: #f0f0f0;
    }
    .determine {
      border: none;
      background-color: #4995dc;
    }
  }
}
</style>

